#ifndef __CAMERA_PACKET_HPP__
#define __CAMERA_PACKET_HPP__

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>

#define FRAME_FIXED_LEN 0x0EU
#define FRAME_HEAD_LEN 0x0CU
#define FRAME_HEADER 0x55FE

namespace gimabl {
    class CameraPacket
    {
    public:
        explicit CameraPacket(uint32_t dataLen);
        ~CameraPacket();
        // Use these two functions when initializing data packets
        int32_t InitPacket(uint16_t header,uint8_t type,uint8_t flag,uint16_t srcId,uint16_t dstId,uint16_t msgId,uint8_t serial);
        int32_t setPayload(uint8_t *loadData, uint8_t loadLen);

        // Use these two functions when parsing data packets
        int32_t setAckData(uint8_t *data, ssize_t len);
        bool ParseData();

        uint8_t *getDataBuf();
        uint32_t getDataLen();

        uint8_t getType();
        uint8_t getFlag();
        uint16_t getMsgId();
        
        uint8_t *getPayloadData();
        uint8_t getPayloadLen();

    private:
        uint16_t combineUint8ToUint16(uint8_t highByte, uint8_t lowByte) ;
        bool isLittleEndian();
        uint16_t toLittleEndian16(uint16_t value); // 小端转换
        void swapEndian(void *data, size_t size);
        uint16_t CRC16Update(uint16_t crc, const uint8_t *data, size_t len);

        uint16_t m_header;          // 帧头
        uint8_t m_type;             // 协议版本
        uint8_t m_flag;             // 数据标志
        uint16_t m_srcId;           //源id
        uint16_t m_dstId;           //目的id
        uint16_t m_msgId;           // 消息ID
        uint8_t m_serial;           // 包序号
        uint8_t m_payloadLen;       // 负载长度
        uint8_t *m_payloadBuf;      // 负载
        uint16_t m_crcCheck;        // X.25 CRC

        uint8_t *m_dataBuf;
        uint32_t m_dataLen;
        bool m_haveData;
        bool m_haveAckData;
    };


} // namespace gimabl
#endif // __CAMERA_PACKET_HPP__